package com.sprocomm.agingtest.vibrator;

import com.sprocomm.agingtest.BaseActivity;
import com.sprocomm.agingtest.Config;
import com.sprocomm.agingtest.R;
import com.sprocomm.agingtest.Tools;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Vibrator;
import android.os.Bundle;
import android.transition.ChangeBounds;
import android.transition.TransitionManager;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class VibratorTest extends BaseActivity {
    private String TAG = "VibratorTest";
    private TextView tvTimes;
    private Button btnStart;
    private LinearLayout mLinearLayout;
//    private int[] bgImages = {R.mipmap.bg_1, R.mipmap.bg_2, R.mipmap.bg_3, R.mipmap.bg_4, R.mipmap.bg_5,
//            R.mipmap.bg_6, R.mipmap.bg_7, R.mipmap.bg_8, R.mipmap.bg_9, R.mipmap.bg_10,
//    };
    private String[] colors = {"#000000","#FF0000","#00FF00","#0000FF","#FFFFFF"};
    private Vibrator vibrator;
    /**
     * 工作的次数
     */
    private int times = 0;
    /**
     * 测试结果
     */
    private static boolean RESULT = false;

    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (vibrator != null && vibrator.hasVibrator()) {
                vibrator.vibrate(Config.VIBRATOR_TIME);//震动1000ms
                changeBackgroundImage();
                times++;
                tvTimes.setText(times + "/" + Config.VIBRATOR_TIMES_PASS);
                //TODO 测试通过
                if (times >= Config.VIBRATOR_TIMES_PASS) {
                    handler.removeCallbacks(runnable);
                    RESULT = true;
                    setPassBtnEnable(true);
                    btnStart.setEnabled(true);
                    times = 0;
                    setBtnsVisibility(View.VISIBLE);
                    return;
                }
            } else {
                // fail
                handler.removeCallbacks(runnable);
                RESULT = false;
                setPassBtnEnable(false);
                setBtnsVisibility(View.VISIBLE);
                return;
            }
            handler.postDelayed(runnable, Config.VIBRATOR_TIME + Config.VIBRATOR_STOP_TIME);//间隔1000ms，再次震动
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vibrator_test);
        //设置屏幕最大亮度
        Tools.setScreenBrightness(WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL, this);
        initView();
        vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);


        setBtnsVisibility(View.INVISIBLE);
    }

    private void initView() {
        tvTimes = (TextView) findViewById(R.id.tv_show_times);
        btnStart = (Button) findViewById(R.id.btn_start);
        mLinearLayout = (LinearLayout) findViewById(R.id.activity_vibrator_test);

    }

    public void start(View view) {
        handler.post(runnable);
        Button button = (Button) view;
        button.setEnabled(false);

    }

    @Override
    protected void onDestroy() {
        Tools.logE("onDestroy");
        handler.removeCallbacks(runnable);
        Tools.setScreenBrightness(WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE, this);
        super.onDestroy();
    }

//    @Override
//    protected void onPause() {
//        Tools.logE("onPause");
//        super.onPause();
//    }
//
//    @Override
//    protected void onStop() {
//        Tools.logE("onStop");
//        super.onStop();
//    }
//
//    @Override
//    protected void onResume() {
//        Tools.logE("onResume");
//        super.onResume();
//    }
//
//    @Override
//    protected void onStart() {
//        Tools.logE("onStart");
//        super.onStart();
//    }
//
//    @Override
//    protected void onRestart() {
//        Tools.logE("onRestart");
//        super.onRestart();
//    }

    public void pass(View view) {
        Tools.exitResultNotic(VibratorTest.this, RESULT);
    }

    private void changeBackgroundImage() {
        int index = (int) (Math.random() * 5);

        TransitionManager.beginDelayedTransition(mLinearLayout, new ChangeBounds());
        //mLinearLayout.setBackground(getResources().getDrawable(bgImages[index], null));
        mLinearLayout.setBackgroundColor(Color.parseColor(colors[index]));
        if (tvTimes != null) {
            int pixel = (int) (Tools.getWinHeight(getWindowManager()) / 20) * (index + 3);
            Tools.setMargins(tvTimes, 0, pixel, 0, 0);
        }


    }

}
